package com.xxl.job.admin.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger配置
 *
 * @author Xumda
 * @version time:2019/12/20 16:50
 */
@Configuration
@EnableSwagger2
public class SwaggerApp {

    private static final String TITLE = "JOB-CENTER 分布式任务调度";

    private static final String AUTHOR = "lixunda";

    private static final String WEB_SITE = "http://lixunda.top/";

    private static final String PACKAGE = "top.lixunda.ecommerce.server.user";

    private static final String EMAIL = "1393886097@qq.com";

    private static final String VERSION = "1.0";

    private static final String DESCRIPTION = "API 描述";

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 为当前包路径
                .apis(RequestHandlerSelectors.basePackage(PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
     *
     * @return ApiInfo
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title(TITLE)
                //创建人
                .contact(new Contact(AUTHOR, WEB_SITE, EMAIL))
                //版本号
                .version(VERSION)
                //描述
                .description(DESCRIPTION)
                .build();
    }

    /**
     * 映射资源文件
     *
     * @param registry ResourceHandlerRegistry
     */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 解决跨域，这里的配置主要针对Swagger
     *
     * @param registry CorsRegistry
     */
    public void addCorsMappings(CorsRegistry registry) {
        // 设置允许跨域的路径
//        registry.addMapping("/swagger-ui.html")
//                // 设置允许跨域请求的域名
//                .allowedOrigins("*")
//                // 是否允许证书 不再默认开启
//                .allowCredentials(true)
//                // 设置允许的方法
//                .allowedMethods("*")
//                // 跨域允许时间
//                .maxAge(3600);
    }
}
